<template>
	<view class="container_index">
		<view class="index_blog">
			<ABlogCard :showComment="true" :item="item" :commentList="commennt"></ABlogCard>
		</view>	
	</view>
</template>
<script>
	import store from '@/store';
	import {
		addComment,
		follow
	} from '@/api/forum.js';
	import {
		likeBlog,
		findBlog,
		getComments,
		likeComment
	} from '@/api/forum.js'
	import {
		mapGetters
	} from "vuex";

	export default {
		computed: mapGetters(['isLogin', 'uid', 'userInfo']),
		onLoad({
			id
		}) {
			this.blogId = id;
			this.init(id)
		},
		beforeDestroy() {
			uni.$emit('reloadlist')
		},
		mounted() {
			uni.$on('getCommentList',(id)=>{
				this.init(id)
			})
		},
		data() {
			return {
				blogId: null,
				item: {},
				commennt: [],
				keywords: '',
				showpop: false,
			}
		},
		methods: {
			handleFollow() {
				follow(this.item.userId, !this.item.isFollowed).then(res => {
					if (res.code === 200) {
						this.init(this.blogId)
					}
				})
			},
			getUrls(images) {
				return images?.split(',')
			},
			handleReback() {
				if (this.rebackContent === "") {
					uni.showToast({
						title: '请输入内容',
						icon: 'none'
					})
					return
				}
				this.commentData.blogId = this.popItem.blogId
				this.commentData.parentId = this.popItem.answerId
				this.addCommentHandle({
					...this.commentData,
					'content': this.rebackContent
				})
			},
			handleShowPop(item) {
				this.popItem = item
				this.showpop = true
			},

			handleComment(item) {
				if (this.keywords === "") {
					uni.showToast({
						icon: 'none',
						title: '请输入内容'
					})
					return
				}
				if (!item) {
					this.commentData.blogId = this.item.id
					this.commentData.parentId = 0
					this.addCommentHandle({
						...this.commentData,
						'content': this.keywords
					})
				}
			},
			addCommentHandle(data) {
				addComment(data).then(res => {
					if (res.code === 200) {
						this.keywords = ""
						this.rebackContent = ""
						this.showpop = false
						this.init(this.blogId)
						for (let k in this.commentData) {
							this.commentData[k] = ""
						}
					}
				})
			},
			init(id) {
				findBlog(id).then(res => {
					this.item = res.data
					getComments(res.data.id).then(res => {
						this.commennt = res.data
					})
				})
			},
			handleUserInfo(item) {
				let jsonString = JSON.stringify(item)
				let encodeURIComponentStr = encodeURIComponent(jsonString)
				uni.navigateTo({
					// 编码
					url: `/pages/forum/userhome/userhome?item=${encodeURIComponentStr}`
				})
			},
			handleDetail(e) {
				uni.navigateTo({
					url: '/pages/forum/detail/blogDetail'
				})
			},
			// 时间格式化
			Timeformat(time) {
				if (time === undefined) return
				let res = time.slice(5, 10) + '日' + time.slice(11, 16)
				return res + '时发布'
			},
			// 点赞
			likeHandle(item) {
				console.log(item);
				if (item.id) {
					likeBlog(item.id).then(res => {
						if (res.code === 200) {
							uni.showToast({
								icon: "none",
								title: "点赞成功",
							})
							// 父组件需重新加载
							// uni.$emit('reloadlist')
							this.init(this.blogId)
						}
					})
				} else if (item.answerId) {
					likeComment(item.answerId).then(res => {
						if (res.code === 200) {
							uni.showToast({
								icon: "none",
								title: "点赞成功",
							})
							// 父组件需重新加载
							// uni.$emit('reloadlist')
							this.init(this.blogId)
						}
					})
				}
			}
		}
	}
</script>